<template>
  <div id="register_back" style="height: 874px" :style="bg">
    <div class="innerDialog" style="padding-top: 64px;">
      <div class="mainBox">
        <el-row class="orderTitle">
          <!--标题-->
          <el-col style="padding-left: 180px;">
            <b>注册本地账户</b>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp">
            <el-input id="Name" placeholder="您的姓名" v-model="userInfo.name"></el-input>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp" style="margin-top: 10px; margin-left: 20px;">
            <el-radio type="radio" label="1" v-model="userInfo.sexInfo">先生</el-radio>
            <el-radio type="radio" label="2" v-model="userInfo.sexInfo">女生</el-radio>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp">
            <el-input id="IdentityNo" placeholder="您的身份证号" @change="checkIdentityNo"
                      v-model="userInfo.identityNo"></el-input>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp">
            <el-input id="Password" placeholder="请输入登录密码" type="password" @change="checkPassword"
                      v-model="userInfo.password"></el-input>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp">
            <el-input id="TelInfo" placeholder="请输入正确的手机号码" type="tel" @change="queryIdentifier"
                      v-model="userInfo.telInfo"></el-input>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp">
            <el-input id="Address" placeholder="您的住址" v-model="userInfo.address"></el-input>
          </el-col>
        </el-row>
        <el-row class="sRow">
          <el-col class="sLbl"></el-col>
          <el-col class="sInp">
            <el-button @click="userRegister" class="sBtn sBtnSuc">注册</el-button>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>


  export default {
    name: "Register",
    inject: ['reload'],
    methods: {
      queryIdentifier() {
        console.log("registerstate" + this.userInfo.registerState);
        if (this.checkTel() === false) return false;
        if (this.checkTelInfo() === false) return false;
        this.$ajax.get('http://localhost:8080/passenger/ispassengerexist?telephone=' + this.userInfo.telInfo).then
        (response => {
          console.log(response);
          if (response.data === 'exist') {
            console.log("TagTelInfo:" + this.tagInfo.telInfo);
            this.userInfo.registerState = 'tel exists';
          } else {
            this.userInfo.registerState = 'clear';
          }
        }).catch(function (error) {
          console.log(error);
          //this.userInfo.registerState = error;
        });
        return true;
      },
      checkTel() {
        if (this.userInfo.telInfo === "") {
          let telText = document.getElementById("Telinfo");
          telText.style.border = "1px solid red";
          telText.focus();
          return false;
        } else {
          return true;
        }
      },
      checkPassword() {
        if (this.userInfo.password === "") {
          let passwordText = document.getElementById("Password");
          passwordText.style.border = "1px solid red";
          passwordText.focus();
          return false;
        } else {
          return true;
        }
      },
      checkIdentityNo() {
        if (this.userInfo.identityNo === '') {
          let identityNo = document.getElementById("IdentityNo");
          identityNo.style.border = "1px solid red";
          identityNo.focus();
          this.tagInfo.identityNo = '请输入身份证号';
          return false;
        }
        else
          return true;
      },
      checkTelInfo() {
        if (this.userInfo.telInfo === '') {
          let telInfo = document.getElementById("TelInfo");
          telInfo.style.border = "1px solid red";
          telInfo.focus();
          this.tagInfo.telInfo = '请输入电话号码';
          return false;
        }
        else
          return true;
      },
      runCheck() {
        let checkResult = this.checkTelInfo();
        console.log("checkresult:" + checkResult);
        checkResult = this.checkIdentityNo() && checkResult;
        checkResult = this.checkPassword() && checkResult;
        console.log("checkresult:" + checkResult);
        console.log("registerState:" + this.userInfo.registerState);
        if (this.userInfo.registerState === 'clear' && checkResult) {
          this.userInfo.checkall = 'true';
          return checkResult && true;
        } else return false;
        /*if (checkResult) return true;
        else return false;*/
      },
      userRegister() {
        console.log("checkall:" + this.userInfo.checkall);
        if (this.runCheck()) {
          console.log('s');
          if (this.userInfo.sexInfo === '1')
            this.userInfo.sexInfo = "男";
          else
            this.userInfo.sexInfo = "女";
          let data = {
            identityno: this.userInfo.identityNo,
            name: this.userInfo.name,
            sex: this.userInfo.sexInfo,
            telephone: this.userInfo.telInfo,
            address: this.userInfo.address,
            password: this.userInfo.password,
            ad: false,
            airlineno: 0
          };

          console.log(data);
          this.$ajax.post('http://localhost:8080/passenger/register', JSON.stringify(data),
            {
              headers: {'Content-Type': 'application/json;charset=UTF-8'}
            }
          ).then(response => {
            console.log(response);
            this.userInfo.password = "";
            this.userInfo.confirmPassword = "";
            if (response.data === 'success') {
              this.openMessage('注册成功', 'success', true);
              //sessionStorage.setItem('after_register_user', this.userInfo.userName);
              this.$router.push({path: '/Account/Login'});
            } else this.openMessage('注册失败，请稍后重试', 'error');
          }).catch(function (error) {
            console.log(error);
            this.userInfo.password = "";
            this.userInfo.confirmPassword = "";
            this.openMessage('链接服务器失败，请稍后重试', 'error');
          });
          //let username = sessionStorage.getItem('user_name');
          //this.$ajax.get(global_.server_host + '/dialog/append/' + username+ '注册');
        } else {
          this.openMessage('注册失败，请稍后重试', 'error');
        }

      },

      openMessage(messageText, messageType, withReload = false) {
        this.$message({
          message: messageText,
          type: messageType
        });
        if (withReload) this.reload();
      }
    },
    mounted() {
    },
    data() {
      return {
        bg: {
          background: 'url(' + require('../../assets/001.jpg') + ') no-repeat right 100px center '
        },
        visible: false,
        userInfo: {
          name: "",
          identityNo: "",
          password: "",
          sexInfo: "3",
          telInfo: "",
          address: "",
          registerState: "",
          checkall: ""
        },
      };
    }
  }
</script>

<style scoped>
  @import "../../resources/account.css";
</style>
